package charlie.shop.warehouse;

import charlie.shop.misc.Common;
import com.charlie.db.DAOLocal;
import com.charlie.db.TopProduct;
import com.charlie.sessions.ProductFacadeLocal;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Creates top products export into Excel
 * 
 * @author Viktoriia Bula
 */
@WebServlet(name = "ReportServlet", urlPatterns = {"/warehouse/report"})
public class ReportServlet extends HttpServlet {

    /**
     * Items per page limit static constant
     */
    final int itemsPerPage = 10;
    @EJB
    private DAOLocal dao;
    @EJB
    private ProductFacadeLocal product;

    /**
     * Get list of top products and display it.
     * <code>GET</code> and
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        int page = Common.getPage(request);
        int from = (page - 1) * itemsPerPage;
        List<TopProduct> data = dao.getProductNameSummCost(from, from + itemsPerPage);
        request.setAttribute("productList", data);
        request.setAttribute("page", page);
        request.setAttribute("pagesCount", (product.count() / itemsPerPage));
        getServletContext().getRequestDispatcher("/warehouse/report.jsp").forward(request, response);
    }

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Get top products data and export it into excel
     * 
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=\"report.xls\"");
        List<TopProduct> data = dao.getProductNameSummCost(0, 100);
        TopProductExport export = new TopProductExport();
        try {
            export.write(data, response.getOutputStream());
        } catch (Exception ex) {
            Logger.getLogger(ReportServlet.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
}
